var rootPath = getRootPath();

$(function() {
	
	$("#resourceAddForm").validate({
		rules : {
			resName : {
				required : true,
				remote : {
					type : "GET",
					url : rootPath + "/resource/checkResName",
					data : {
						resName : function() {
							return $("#resName").val();
						}
					},
					dataFilter : function(data,type) {
						if(data == "true") {
							return true;
						} else {
							return false;
						}
					}
				}
			}
		},
		messages: {
			resName:{
				remote : "资源名称已存在"
			}
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			$.ajax({
				url: rootPath + '/resource/addResource',
	            type: 'POST',
	            data: JSON.stringify($('#resourceAddForm').serializeJson()),
	            contentType: 'application/json;charset=UTF-8',
	            success: function (data) {
	            	if("0000" == data.returnCode) {	            		
	            		layer.msg('添加成功!',{icon:1,time:1000},function() {
	            			parent.window.location.href = rootPath + '/resourcePage';
	            			var index = parent.layer.getFrameIndex(window.name);
	            			parent.$('.btn-refresh').click();
	            			parent.layer.close(index);
	            		});
	            	} else {
	            		layer.alert(data.returnMessage);
	            	}
	            },
	            error: function (jqXHR, textStatus, errorThrown) {
	            	layer.msg('error!',{icon:1,time:1000});
	            }
	        });
		}
	});
});